package cn.ticket.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Notice;
import cn.ticket.service.NoticeService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("notice")
public class NoticeContoller {
	@Autowired
	NoticeService noticeService;
	@RequestMapping("")
	public String noticeIndex(){
		return "notice";
	}
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody Notice notice){
		Map<String, Object> result=new HashMap<>();
		int num=noticeService.insertSelective(notice);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String update(@RequestBody Notice notice){
		Map<String, Object> result=new HashMap<>();
		int num=noticeService.updateByPrimaryKey(notice);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String delete(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		int id=Integer.parseInt(args.get("id"));
		int num=noticeService.deleteByPrimaryKey(id);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getAll(){
		Map<String, Object> result=new HashMap<>();
		List<Notice> notices=noticeService.getAll();
		if (!notices.isEmpty()) {
			result.put("status", 1001);
			result.put("body", notices);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getByCount",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByCount(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		int start=Integer.parseInt(args.get("start"));
		int count=Integer.parseInt(args.get("count"));
		List<Notice> notices=noticeService.getByCount(start, count);
		if (!notices.isEmpty()) {
			result.put("status", 1001);
			result.put("body", notices);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
}
